package ws;

import java.util.Properties;

import gestionFichiersEJBImpl.GestionFichiersEJBRemote;

import javax.jws.WebService;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

import javax.xml.ws.soap.MTOM;

@MTOM
@WebService(endpointInterface = "ws.GestionFichiersInterfaceWS")
public class GestionFichiersInterfaceWSImpl implements GestionFichiersInterfaceWS{
		
	@Override
	public boolean uploadAFile(byte[] contenuFichier, byte[] name) {
		
		Properties properties = new Properties();
		properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
		properties.put( Context.SECURITY_PRINCIPAL, "jonas" );
		properties.put( Context.SECURITY_CREDENTIALS, "jonas" );
		properties.setProperty( Context.PROVIDER_URL , "rmi://172.20.128.135:1099" );
		properties.setProperty( Context.URL_PKG_PREFIXES , "org.objectweb.jonas.naming" );
		Context context = null;
		
		try {
			context = new InitialContext( properties );
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		try {
			
			
			NamingEnumeration<NameClassPair> list = context.list("rmi://172.20.128.135:1099");
			
			while (list.hasMoreElements()) {
				String type = list.nextElement().getNameInNamespace();
				System.out.println(type);
			}
			
			
		} catch (NamingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// test des méthodes
		GestionFichiersEJBRemote ejbref = null;
		try {
			ejbref = (GestionFichiersEJBRemote) context.lookup("GestionFichiersEJB");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Dans le web service");
		
		
		/*boolean reussite = false;
		
		String nomFichier = new String(name);
		
		File fichierRecu = new File("/opt/pad/NicolasScheubel/Fichiers/"+nomFichier);
		
		if(fichierRecu != null){
			
			reussite = true;
			
		}
		
		try {
			
			FileOutputStream fileOut = new FileOutputStream(fichierRecu);
			fileOut.write(contenuFichier);
			fileOut.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*if(em.isOpen()==false){
			
			System.out.println("Pas encore de BDD");
			
		}*/
		//ejbref.uploadAFile(contenuFichier, name);
		
	//	System.out.println("Fichier chargé !!!");
		
		return true;
		
	}

	@Override
	public String downloadAFile(String name) {
		// TODO Auto-generated method stub
		//gestionFichierRef.DownloadAFile(name);
		return null;
	}

	@Override
	public boolean saveAFile(String name) {
		// TODO Auto-generated method stub
		//gestionFichierRef.SaveAFile(name);
		return false;
	}

	@Override
	public boolean createDirectory(String name) {
		// TODO Auto-generated method stub
		//gestionFichierRef.CreateDirectory(name);
		return false;
	}

	@Override
	public boolean compress(String name) {
		// TODO Auto-generated method stub
		//gestionFichierRef.Compress(name);
		return false;
	}

	@Override
	public boolean createFile(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteFile(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteDirectory(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean editFile(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean rename(String name, String newName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean chmod(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean open() {
		// TODO Auto-generated method stub
		return false;
	}


}
